package com.dqx.opengl_es_demo.model.objloader;

import android.content.res.Resources;
import android.graphics.Color;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * 加载材质的工具类
 */
public class MtlLoaderUtil {

    /**
     * 加载并解析材质
     *
     * @param fname assets的mtl文件路径
     * @param res
     * @return
     */
    public static HashMap<String, Mtl> load(String fname, Resources res) {
        HashMap<String, Mtl> mMTLMap = new HashMap<>();
        if (res == null || TextUtils.isEmpty(fname))
            return mMTLMap;
        //材质对象
        Mtl mtl = null;
        try {
            //读取assets下文件
            InputStream in = res.getAssets().open(fname);
            InputStreamReader isr = new InputStreamReader(in);
            BufferedReader buffer = new BufferedReader(isr);
            //行数据
            String line;
            //循环读取每一行的数据
            while ((line = buffer.readLine()) != null) {
                //跳过注释和空行
                if (line.length() == 0 || line.charAt(0) == '#') {
                    continue;
                }
                //以空格分割字符串
                StringTokenizer parts = new StringTokenizer(line, " ");
                int numTokens = parts.countTokens();
                //跳过空部分
                if (numTokens == 0) {
                    continue;
                }

                //打头的字符
                String type = parts.nextToken();
                //去制表符
                type = type.replaceAll("\\t", "");
                //去空格
                type = type.replaceAll(" ", "");

                if (type.equals("newmtl")) {//材质
                    String name = parts.hasMoreTokens() ? parts.nextToken() : "def";
                    // 将上一个对象加入到列表中
                    if (mtl != null) {
                        mMTLMap.put(mtl.name, mtl);
                    }
                    //创建材质对象
                    mtl = new Mtl();
                    //材质对象名称
                    mtl.name = name;
                } else if (type.equals("Ka")) {//环境光
                    mtl.Ka = getColorFromParts(parts);
                } else if (type.equals("Kd")) {//散射光
                    mtl.Kd = getColorFromParts(parts);
                } else if (type.equals("Ks")) {//镜面光
                    mtl.Ks = getColorFromParts(parts);
                } else if (type.equals("Ns")) {//高光调整参数
                    String ns = parts.nextToken();
                    mtl.Ns = Float.parseFloat(ns);
                } else if (type.equals("d") || type.equals("Tr")) {//透明度
                    mtl.D = Float.parseFloat(parts.nextToken());
                } else if (type.equals("Ni")) {//折射值
                    String ni = parts.nextToken();
                    mtl.Ni = Float.parseFloat(ni);
                } else if (type.equals("map_Ka")) {//环境光的纹理贴图
                    mtl.map_Ka = parts.nextToken();
                } else if (type.equals("map_Kd")) {//散射光的纹理贴图
                    mtl.map_Kd = parts.nextToken();
                } else if (type.equals("map_Ks")) {//镜面光的纹理贴图
                    mtl.map_Ks = parts.nextToken();
                } else if (type.equals("map_Ns")) {//反射光的纹理贴图
                    mtl.map_Ns = parts.nextToken();
                } else if (type.equals("map_d") || type.equals("map_Tr")) {//alpha通道纹理贴图
                    mtl.map_d = parts.nextToken();
                } else if (type.equals("map_Bump")) {//凹凸贴图
                    mtl.map_bump = parts.nextToken();
                }
            }
            if (mtl != null)
                mMTLMap.put(mtl.name, mtl);
            buffer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mMTLMap;
    }

    /**
     * 返回一个oxffffffff格式的颜色值
     *
     * @param parts
     * @return
     */
    private static int getColorFromParts(StringTokenizer parts) {
        int r = (int) (Float.parseFloat(parts.nextToken()) * 255f);
        int g = (int) (Float.parseFloat(parts.nextToken()) * 255f);
        int b = (int) (Float.parseFloat(parts.nextToken()) * 255f);
        return Color.rgb(r, g, b);
    }

}


